import 'dart:async';
// import 'dart:html';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/get.dart';
import 'package:m6/Public/manager/app_manager.dart';
import 'package:m6/Untils/api.dart';
import 'package:m6/Untils/request.dart';
import 'package:m6/Untils/toast.dart';
import 'package:m6/main.dart';

class LoginController extends GetxController {
  String phonePrefix = "52";
  var phone = ''.obs;
  var code = ''.obs;

  var isValidPhoneAndCode = false.obs;

  var countDown = 60.obs; //倒计时
  var isTimerActive = false;
  // var countDownButtonTitle = "Conseguir".obs;

  final phoneInputLength = 10;
  final codeInputLength = 4;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
  }

  void checkPhoneString(String phoneString) {
    phone.value = phoneString;
    checkCanLogin();
  }

  void checkCodeString(String codeString) {
    code.value = codeString;
    checkCanLogin();
  }

  void checkCanLogin() {
    if (phone.isNotEmpty &&
        phone.toString().length == phoneInputLength &&
        code.isNotEmpty &&
        code.toString().length == codeInputLength) {
      isValidPhoneAndCode = RxBool(true);
    } else {
      isValidPhoneAndCode = RxBool(false);
    }
  }

  void startCountDown() {
    if (isTimerActive) {
      return;
    }
    Timer.periodic(const Duration(seconds: 1), (timer) {
      if (countDown <= 0) {
        countDown.value = 60;
        isTimerActive = false;
        timer.cancel();
      } else {
        isTimerActive = true;
        countDown--;
      }
    });

    sendCode(1);
  }

//发送验证码
  Future<void> sendCode(int smsType) async {
    final params = {
      "mobile": phone.value,
      "smsType": smsType.toString(),
      "flowCompanyCode": "",
      "flowBindId": "",
      "deciveBrand": "Apple",
      "flowIdfa": "", //ACTrackData.shared.advertisingId,
      "flowAdid": "", //Adjust.adid() as Any,
      "flowIdfv": "", //UIDevice.current.identifierForVendor?.uuidString as Any
    };

    MToast.ShowLoading(null);
    var url = smsType == 1 ? URL_Send_Code_SMS : URL_Send_Code_Voice;
    CustomerResponse response = await Request.post(url, params);
    MToast.HiddenLoading();
    if (response.code != 1) {
      MToast.ShowMessage(response.msg);
    }
  }

  Future<void> loginRequest() async {
    final params = {
      "mobile": "$phonePrefix${phone.value}",
      "password": "123456",
      "smsCode": "",
      "inviteCode": "",
      "appVersion": "1.0",
      "appPackage": "com.YuCTime.Pago",
      "deciveBrand": "Apple",
      "deciveId": "",
      "wifiStr": "",
      "flowCompanyCode": "adjust",
      "flowBindId": "",
      "pushPlatForm": "firebase",
      "pushBindId": "",
      "flowIdfa": "",
      "flowAdid": "",
      "flowIdfv": ""
    };
    MToast.ShowLoading(null);
    CustomerResponse response = await Request.post(URL_Login, params);
    MToast.HiddenLoading();
    if (response.code == 1) {
      final AppManager ma = Get.find();
      ma.isLogin.value = true;
      ma.session = response.data?["sessionId"];
      ma.update(["main_home_id"]);
    } else {
      MToast.ShowMessage(response.msg);
    }
  }
}
